package com.sne.airways.customer.service.impl;

import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.basecore.service.impl.BaseServiceImpl;
import com.sne.airways.customer.dao.CustDAO;
import com.sne.airways.customer.dto.TdCustDTO;
import com.sne.airways.customer.pojo.TdCust;
import com.sne.airways.customer.service.CustService;
import com.sne.airways.system.annotation.MethodLog;
import com.sne.airways.system.enums.ModuleEnum;
import com.sne.airways.system.enums.OpTypeEnum;
import com.sne.airways.system.utils.AppUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-12-22
 * Time: 下午6:07
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustServiceImpl extends BaseServiceImpl implements CustService {
    @Autowired
    private CustDAO custDAO;
    @Override
    public List<TdCustDTO> queryCustList(int start, int limit, String custName, String custPhone,String custType) throws AppException{
        List<TdCust> custList = custDAO.queryCustList(start,limit,custName,custPhone,custType);
        List<TdCustDTO> custDTOList = new ArrayList<TdCustDTO>();
        for(TdCust cust : custList){
            TdCustDTO custDTO = new TdCustDTO();
            String[] ignoreProperties = {"createUser","modifyUser"};
            BeanUtils.copyProperties(cust,custDTO,ignoreProperties);
            custDTOList.add(custDTO);
        }
        return custDTOList;
    }

    @Override
    public Integer queryCustCount(String custName, String custPhone,String custType) throws AppException{
        return custDAO.queryCustCount(custName,custPhone,custType);
    }

    @Override
    @MethodLog(opType = OpTypeEnum.添加客户,module = ModuleEnum.客户管理)
    public void custSave(String custName, String custRIF, String custCode, String enName, String phone, String custType,
                           String hsCode, String EDICode, String fax, String tix, String email, String contract, String address,
                           String level, int limitContract, String status, String remark, int version) throws AppException {
        int count = custDAO.queryCustCount(custName,null,null);
        if(count>0){
            throw new AppException("用户已经存在");
        }
        TdCust cust = new TdCust();
        cust.setCustName(custName);
        cust.setCustRIF(custRIF);
        cust.setCustCode(custCode);
        cust.setEnName(enName);
        cust.setPhone(phone);
        cust.setCustType(custType);
        cust.setHsCode(hsCode);
        cust.setEDICode(EDICode);
        cust.setFax(fax);
        cust.setTix(tix);
        cust.setEmail(email);
        cust.setContract(contract);
        cust.setAddress(address);
        cust.setLevel(level);
        cust.setLimitContract(limitContract);
        cust.setStatus(status);
        cust.setRemark(remark);
        cust.setVersion(version);
        cust.setCreateDate(new Date());
        cust.setCreateUser(AppUtils.getCurrentUser());
        saveObject(cust);
        TdCustDTO custDTO = new TdCustDTO();
        String[] ignoreProperties = {"createUser","modifyUser"};
        BeanUtils.copyProperties(cust, custDTO, ignoreProperties);
        custDTO.setCreateUser(cust.getCreateUser() == null ? null : cust.getCreateUser().getUserName());
        custDTO.setModifyUser(cust.getModifyUser() == null ? null : cust.getModifyUser().getUserName());
    }

    @Override
    @MethodLog(opType = OpTypeEnum.编辑客户,module = ModuleEnum.客户管理)
    public void custUpdate(String custId, String custName, String custRIF, String custCode, String enName, String phone,
                           String custType, String hsCode, String EDICode, String fax, String tix, String email, String contract,
                           String address, String level, int limitContract, String status, String remark, int version) throws AppException {
        TdCust cust = loadObject(TdCust.class,custId);
        isCurrentVersion(cust,version);
        cust.setCustName(custName);
        cust.setCustRIF(custRIF);
        cust.setCustCode(custCode);
        cust.setEnName(enName);
        cust.setPhone(phone);
        cust.setCustType(custType);
        cust.setHsCode(hsCode);
        cust.setEDICode(EDICode);
        cust.setFax(fax);
        cust.setTix(tix);
        cust.setEmail(email);
        cust.setContract(contract);
        cust.setAddress(address);
        cust.setLevel(level);
        cust.setLimitContract(limitContract);
        cust.setStatus(status);
        cust.setRemark(remark);
        cust.setVersion(version);
        cust.setModifyDate(new Date());
        cust.setModifyUser(AppUtils.getCurrentUser());
        updateObject(cust);
    }

    @Override
    @MethodLog(opType = OpTypeEnum.删除客户,module = ModuleEnum.客户管理)
    public void custDel(String custId, int version) throws AppException {
        TdCust cust = loadObject(TdCust.class,custId);
        cust.setVersion(version);
        deleteObject(cust);
    }
}
